

//ztree
var setting = {
    //data:{simpleData:{enable:true,idKey:"id",pIdKey:"parentId",rootPId:'-1'}},
    callback:{onClick:function(event, treeId, treeNode){
        var id = treeNode.id;
        var obj={"id":id};
        treetable.params=obj;
        treetable.reload();
    }},
    async: {
        type:'post',
        enable: true,
        url:base+"/organization/view/treeData"
    }
};
function refreshTree(){
    $.fn.zTree.init($("#ztree"), setting, null);

}

function refreshTreeTable(){
    	var obj={"id":-1};
        treetable.params=obj;
        treetable.reload();
}



function loadTreeTable(){
    treetable= $("#treeTable").treeTable({
        url:base + "/organization/view/list",
        column: 1,
        expandable: true,
        theme:"default",
        columns:[
            {field:"name",title:"机构名称",width:'160'},
            {field:"area",title:"归属区域",algin:"left",formatter:function(value){
                if(value.area == null){
                    return "";
                }else{
                    return value.area.name;
                }
            }},
            {field:"code",title:"机构编码",algin:"center"},
            {field:"type",title:"类型",algin:"center",formatter:function(value){
                if('1' == value.type){
                    return "公司";
                }else if('2' == value.type){
                    return "部门";
                }else if('3' == value.type){
                    return "小组";
                }
            }},
            {field:"description",title:"备注",algin:"left"},
            {field:"isEnabled",title:"状态",algin:"center",formatter:function (value){
                return value.isEnabled=='1'?"启用":"禁用"
            }},
            {title:"操作",align:'center',formatter:function(value){
                var Html='<div class="btn-group">'
                    +'<a class="btn btn-sm purple" onclick="doEdit(\''+value.id+'\')">编辑</a>'
                    +'<a class="btn btn-sm btn-danger" onclick="doDelete(\''+value.id+'\')">删除</a>'
                    +'<a class="btn btn-sm btn-primary" onclick="doAdd(\''+value.id+'\')">添加下级</a>'
                    +'</div>';
                return Html;
            }}
        ]
    });
}


function doSubmit(type) {
    if (!formCheck().form()) {
        return;
    }
    var url;

    if ("add" == type) {
        url = base + "/organization/sk/insert";
    } else {
        url = base + "/organization/uc/update";
    }
    $.post(url, $("#editForm").serialize(), function (data) {
        $.messager.alert0({selector: "#editAlert", type: data.code, html: data.message});
        if (data && data.code == 'SUCCESS') {
            tools.goto(base + "/organization/view/index", 500);
        }
    }, "json");

}


function formCheck() {
    return $('#editForm').validate({
        rules: {
            name: {
                maxlength:[24],
                required:true
            },
            code: {
                digits:true,
                maxlength:[4],
                required:true
            },
            sort: {
                maxlength:[8],
                digits:true

            },
            address: {
                maxlength:[100]

            },
            zipCode: {
                pattern:"[0-9]{6}"

            },
            phone: {
                phone:true

            },
            email: {
                email:true
            },
            areaName:{
                required:true
            }
        }
    });
}

/**
 *删除
 */
function doDelete(id) {

    if(!id){
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
    var url =base+"/organization/dh/delete";
    $.messager.confirm("你确定要删除选中的记录吗?", function (ck) {
        if(ck){
            var ary=[];
            ary.push(id);
            $.post(url, {ids: ary}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.code, html: data.message});
                if (data && data.code == 'SUCCESS') {
                    treetable.reload();
                }
            });
        }})
}


function doEdit(id) {
    var url = base+ "/organization/uc/edit?id=" + id + "&type=edit";
    window.location.href=url;
}

function doAdd(id) {
    var url =base+ "/organization/uc/edit?id=" + id + "&type=add";
    window.location.href=url;
}

